package com.itany.shop.service.Proxy;

import com.itany.shop.entity.Product;
import com.itany.shop.exception.DataAccessException;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.ServiceException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.tran.TransactionManager;

import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class ProductServiceProxy implements ProductService {
    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productServiceTarget");
    @Override
    public List<Product> findAll() {
        try {
            tran.begin();
            List<Product> products = productService.findAll();
            tran.commit();
            return products;
        } catch (DataAccessException e) {
            tran.rollback();
            throw new ServiceException(e);
        }
    }
    @Override
    public List<Product> findPage(String pageNo, String pageSize) {
        try {
            tran.begin();
            List<Product> products = productService.findPage(pageNo, pageSize);
            tran.commit();
            return products;
        }catch (DataAccessException e) {
            tran.rollback();
            throw new ServiceException(e);
        }
        catch (ProductNotExistException e) {
            tran.rollback();
            throw e;
        }
    }
}
